<template>
  <div class="asset-inventory-page">
    <el-card>
      <div style="margin-bottom: 16px; display: flex; justify-content: space-between; align-items: center;">
        <span style="font-weight: bold;">资产盘点任务管理</span>
        <el-button type="primary" size="small" @click="onAdd">新增盘点任务</el-button>
      </div>
      <el-table :data="list" border stripe style="width: 100%;">
        <el-table-column prop="id" label="ID" width="60" />
        <el-table-column prop="taskName" label="任务名称" min-width="120" />
        <el-table-column prop="rangeDesc" label="盘点范围" min-width="160" />
        <el-table-column prop="principal" label="负责人" min-width="100" />
        <el-table-column prop="planDate" label="计划时间" min-width="120" />
        <el-table-column prop="status" label="状态" min-width="80">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">{{ scope.row.status === 1 ? '已完成' : '未完成' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="120" />
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button size="small" type="danger" @click="onDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 新增弹窗 -->
    <el-dialog v-model="dialog.visible" title="新增盘点任务" width="400px">
      <el-form :model="dialog.form" label-width="100px">
        <el-form-item label="任务名称" required>
          <el-input v-model="dialog.form.taskName" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="盘点范围">
          <el-input v-model="dialog.form.rangeDesc" placeholder="请输入盘点范围" />
        </el-form-item>
        <el-form-item label="负责人">
          <el-input v-model="dialog.form.principal" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="计划时间">
          <el-date-picker v-model="dialog.form.planDate" type="date" placeholder="选择日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="dialog.form.remark" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialog.visible = false">取消</el-button>
        <el-button type="primary" @click="doSave">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'

const list = ref([])
const fetchList = async () => {
  const res = await request.get('/asset/inventory/list')
  if (res.code === 1) {
    list.value = res.data
  }
}
const dialog = reactive({ visible: false, form: { taskName: '', rangeDesc: '', principal: '', planDate: '', remark: '' } })
const onAdd = () => {
  dialog.form = { taskName: '', rangeDesc: '', principal: '', planDate: '', remark: '' }
  dialog.visible = true
}
const doSave = async () => {
  if (!dialog.form.taskName) {
    ElMessage.warning('任务名称不能为空')
    return
  }
  await request.post('/asset/inventory', dialog.form)
  ElMessage.success('新增成功')
  dialog.visible = false
  fetchList()
}
const onDelete = (row) => {
  ElMessageBox.confirm('确认删除该任务？', '提示', { type: 'warning' }).then(async () => {
    await request.delete(`/asset/inventory/${row.id}`)
    ElMessage.success('删除成功')
    fetchList()
  })
}
onMounted(() => {
  fetchList()
})
</script>

<style scoped>
.asset-inventory-page {
  padding: 24px;
}
</style> 